﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;

namespace ETL.Tools
{
	public class FTP
	{

		public static void UploadData(string FTPServerIP, string FTPUserID, string FTPPassword, string Filename)
		{
			Stream Stream = null;
			FileInfo FileInf = new FileInfo(Filename);
			string uri = "ftp://" + FTPServerIP + "/" + FileInf.Name;
			FtpWebRequest ReqFTP;
			ReqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
						"ftp://" + FTPServerIP + "/" + FileInf.Name));
			ReqFTP.Credentials = new NetworkCredential(FTPUserID,
														 FTPPassword);
			ReqFTP.KeepAlive = false;
			ReqFTP.Method = WebRequestMethods.Ftp.UploadFile;
			ReqFTP.UseBinary = true;

			ReqFTP.ContentLength = FileInf.Length;

			int BuffLength = 2048;
			byte[] Buff = new byte[2048];
			int ContentLen;

			FileStream FS = FileInf.OpenRead();
			long FileLength = FileInf.Length;
			long Done = 0;
			long DoneAct = 0;
			try
			{
				Stream = ReqFTP.GetRequestStream();
				ContentLen = FS.Read(Buff, 0, BuffLength);
				DoneAct += ContentLen;
				while (ContentLen != 0)
				{
					Stream.Write(Buff, 0, ContentLen);
					ContentLen = FS.Read(Buff, 0, BuffLength);
					DoneAct += ContentLen;
					if (Done < (DoneAct * 100 * 100 / FileLength))
					{
						Done = (DoneAct * 100 * 100 / FileLength);
					}
				}
			}
			finally
			{
				if (Stream != null)
				{
					Stream.Close();
				}
				if (FS != null)
				{
					FS.Close();
				}
			}
		}

	}
}
